﻿using System;
using Eisk.BusinessEntities;
using Eisk.BusinessLogicLayer;

namespace Eisk.Web
{
    public partial class add_supplier : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request.QueryString["supplierId"]))
                {
                    string supplierID = Request.QueryString["supplierId"];
                    int supplierId = Convert.ToInt16(supplierID);

                    Supplier newSupplier = new SupplierBLL().GetSupplierBySupplierId(supplierId);

                    if (newSupplier != null)
                    {
                        TextBoxName.Text = newSupplier.Name;
                        TextBoxAddress.Text = newSupplier.Address;
                        TextBoxPhone.Text = newSupplier.Phone;
                        TextBoxEmail.Text = newSupplier.Email;
                        TextBoxZip.Text = newSupplier.Zip;
                        ButtonSaveSupplier.Text = "Update";
                        LiteralType.Text = "Edit";
                    }

                }
            }
        }

        protected void ButtonSaveSupplier_Click(object sender, EventArgs e)
        {
            try
            {
                string name;
                string address;
                string phone;
                string email;
                string zip;
                UIDataCollection(out name, out address, out phone, out email, out zip);

                Supplier newSupplier = null;
                SupplierBLL aSupplyBll = new SupplierBLL();

                if (!string.IsNullOrEmpty(Request.QueryString["supplierId"]))
                {
                    string supplierID = Request.QueryString["supplierId"];
                    int supplierId = Convert.ToInt16(supplierID);

                    newSupplier = aSupplyBll.GetSupplierBySupplierId(supplierId);
                }

                if (newSupplier != null)
                {
                    CollectSupplier(name, address, phone, email, zip, newSupplier);

                    aSupplyBll.UpdateSupplier(newSupplier);
                    Response.Redirect("view-suppliers.aspx");
                }
                else
                {
                    newSupplier = new Supplier();

                    if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(email))
                    {
                        CollectSupplier(name, address, phone, email, zip, newSupplier);

                        aSupplyBll.CreateNewSupplier(newSupplier);
                        Response.Redirect("view-suppliers.aspx");
                    }
                }                
            }
            catch (Exception ex)
            {
                LiteralStatus.Text = "An internal Error occured!";
            }
        }

        private void UIDataCollection(out string name, out string address, out string phone, out string email, out string zip)
        {
            name = string.Empty;
            address = string.Empty;
            phone = string.Empty;
            email = string.Empty;
            zip = string.Empty;

            name = TextBoxName.Text.Trim();

            address = TextBoxAddress.Text.Trim();

            phone = TextBoxPhone.Text.Trim();

            email = TextBoxEmail.Text.Trim();

            zip = TextBoxZip.Text.Trim();

        }

        private static void CollectSupplier(string name, string address, string phone, string email, string zip, Supplier newSupplier)
        {
            if (!string.IsNullOrEmpty(name))
            {
                newSupplier.Name = name;
            }
            if (!string.IsNullOrEmpty(address))
            {
                newSupplier.Address = address;
            }
            if (!string.IsNullOrEmpty(phone))
            {
                newSupplier.Phone = phone;
            }
            if (!string.IsNullOrEmpty(email))
            {
                newSupplier.Email = email;
            }
            if (!string.IsNullOrEmpty(zip))
            {
                newSupplier.Zip = zip;
            }
        }
        protected void LinkButtonViewSuppliers_Click(object sender, System.EventArgs e)
        {
            Response.Redirect("view-suppliers.aspx");
        }
    }
}